<template>
	<view class="ai">
		<headerinfo></headerinfo>
		<view class="width60" style="width: 80%;">
			<view class="" style="padding: 20px;">
				<el-button type="primary" style="margin-bottom: 20px;" @click="add">新增</el-button>
				<el-button type="primary" style="margin-bottom: 20px;" @click="searchWork">已分配任务</el-button>
				<el-button type="primary" style="margin-bottom: 20px;" @click="wheel">随机开始任务</el-button>
				<el-table border :data="tableData" style="width: 100%">
					<el-table-column prop="title" label="标题">
					</el-table-column>
					<el-table-column prop="priority" label="状态" width="140">
					</el-table-column>
					<el-table-column prop="category" label="分类" width="140">
					</el-table-column>
					<el-table-column prop="description" label="描述">
					</el-table-column>
					<el-table-column fixed="right" label="操作" width="220">
						<template slot-scope="scope">
							<el-button @click="start(scope.row)" type="text" size="small">开始</el-button>
							<el-button type="text" size="small" @click="over(scope.row)">完成</el-button>
							<el-button type="text" size="small" @click="setTime(scope.row)">配置时间</el-button>
							<el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
							<el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
			</view>

		</view>

		<el-dialog :title="formtitle" :visible.sync="formDia" width="50%">
			<view>
				<el-form ref="ruleForm" :rules="rules" :model="form" label-width="100px">
					<el-form-item label="任务标题" prop="title">
						<el-input v-model="form.title"></el-input>
					</el-form-item>
					<el-form-item label="任务优先级" prop="priority">
						<el-select v-model="form.priority" placeholder="请选择">
							<el-option v-for="item in priorityList" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="任务分类">
						<el-select v-model="form.category" placeholder="请选择">
							<el-option v-for="item in worklist" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="任务描述" prop="description">
						<el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="form.description">
						</el-input>
					</el-form-item>
				</el-form>
			</view>
			<span slot="footer" class="dialog-footer">
				<el-button @click="formDia = false">取 消</el-button>
				<el-button type="primary" @click="onSubmit">确 定</el-button>
			</span>
		</el-dialog>


		<el-dialog title="设置时间" :visible.sync="dateDia" width="50%">
			<view>
				<el-form ref="ruleForm" label-width="80px">
					<el-form-item label="任务时间" prop="title">
						<el-date-picker v-model="date" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
						</el-date-picker>
					</el-form-item>
				</el-form>
			</view>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dateDia = false">取 消</el-button>
				<el-button type="primary" @click="setTimeOk">确 定</el-button>
			</span>
		</el-dialog>
	</view>
</template>

<script>
	import headerinfo from '@/components/headerinfo'
	import {
		handleShowEffects
	} from '@/js_sdk/firework-effects/effect.js'
	export default {
		components: {
			headerinfo
		},
		data() {
			return {
				date: '',
				dateDia: false,
				checkItem: {},
				formtitle: '新增',
				form: {
					category: '',
					description: '',
					priority: '',
					title: '',
				},
				formDia: false,
				priorityList: [{
					label: '紧急重要',
					value: 'HIGH_HIGH'
				}, {
					label: '紧急但不重要',
					value: 'HIGH_LOW'
				}, {
					label: '不紧急但重要',
					value: 'LOW_HIGH'
				}, {
					label: '不紧急不重要',
					value: 'LOW_LOW'
				}],
				worklist: [],
				tableData: [],
				rules: {
					title: [{
						required: true,
						message: '请输入任务标题',
						trigger: 'blur'
					}],
					description: [{
						required: true,
						message: '请输入任务描述',
						trigger: 'blur'
					}],
					priority: [{
						required: true,
						message: '请选择任务优先级',
						trigger: 'change'
					}],

				}
			};
		},
		onLoad(e) {
			this.getData()
			this.getTaskCategory()
		},
		mounted() {
			this.fangyanhua()
		},
		methods: {
			wheel(){
				let data = {
					date:'',
					onlyChooseNotStarted:false
				}
				this.$api2.wheel(data).then(res => {
					console.log(res)
					let title = '随机到任务：'+res.title+' 是否开始？'
					this.$confirm(title, '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						
						this.$api2.startTask({taskId: res.docId}).then(res => {
							this.$message.success('任务开始')
							this.getData()
						})
						
						
						// let data = {
						// 	taskId: e.docId
						// }
						// this.$api2.removeTaskFromPool(data).then(res => {
						// 	this.$message.success('删除成功')
						// 	this.getData()
						// })
					})
					
				})
			},
			setTime(e) {
				console.log(e)
				this.dateDia = true
				this.checkItem = e
			},
			setTimeOk() {
				if (this.date == '') {
					return this.$message.warning('请选择时间')
				}

				let data = {
					taskIds: [this.checkItem.docId],
					date: this.date
				}
				this.$api2.assignTasksToDay(data).then(res => {
					this.$message.success('设置成功')
					this.getData()
					this.dateDia = false
				})
			},
			fangyanhua() {
				this.$message.success('恭喜自己完成所有任务')
				handleShowEffects({
					type: 'schoolpride' //fireworks schoolpride realisticlook stars all
				})
			},
			add() {
				this.form = {
					category: '',
					description: '',
					priority: '',
					title: '',
				}
				this.formtitle = '新增'
				this.formDia = true
			},
			start(e) {
				let data = {
					taskId: e.docId
				}
				this.$api2.startTask(data).then(res => {
					this.$message.success('任务开始')
					this.getData()
				})
			},
			over(e) {
				let data = {
					taskId: e.docId
				}
				this.$api2.finishTask(data).then(res => {
					this.$message.success('任务结束')
					this.getData()
				})
			},
			del(e) {
				this.$confirm('是否确认删除?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					let data = {
						taskId: e.docId
					}
					this.$api2.removeTaskFromPool(data).then(res => {
						this.$message.success('删除成功')
						this.getData()
					})
				})

			},
			getTaskCategory() {
				this.$api2.getTaskCategory().then(res => {
					console.log(res)
				})
			},
			getData() {
				this.$api2.getPoolTasks().then(res => {
					console.log('任务')
					console.log(res)
					this.tableData = res
				})
			},
			searchWork() {
				uni.navigateTo({
					url: '/pages/work/has'
				})
				// this.$api2.getAssignedTasks().then(res => {
				// 	console.log('任务')
				// 	console.log(res)
				// 	this.tableData = res
				// })
			},
			edit(e) {
				console.log(e)
				this.form = e
				this.formtitle = '编辑'
				this.formDia = true
			},
			onSubmit() {
				this.$refs['ruleForm'].validate((valid) => {
					if (valid) {

						if (this.form.docId) {
							this.$api2.updateTask(this.form).then(res => {
								console.log(res)
								this.formDia = false
								this.$message.success('编辑成功')
								this.getData()
							})
						} else {
							this.$api2.addTaskToPool(this.form).then(res => {
								console.log(res)
								this.formDia = false
								this.$message.success('添加成功')
								this.getData()
							})
						}

					}
				});
			}
		}
	};
</script>


<style lang="scss" scoped>
</style>